﻿#include "BaseType.h"
#include "Tracer.h"
#include "Gpio.h"
using namespace libemb;

#include <stdlib.h>

void printGpioTestMenu()
{
    TRACE_YELLOW("================HELP=================\n");
    TRACE_YELLOW("get : get gpio pin value command.\n");
    TRACE_YELLOW("set : set gpio pin value command.\n");
    TRACE_YELLOW("quit: \'q\' or \'quit\'.\n");
    TRACE_YELLOW("-------------------------------------\n");
}


void TestGpio()
{	
    int ret;
    std::string inputStr;
    while (1)
    {
        printGpioTestMenu();
        std::cin >> inputStr;
        std::cout << inputStr << std::endl;
        if ("q" == inputStr || "quit" == inputStr)
        {
            break;
        }
        else if ("get" == inputStr)
        {
            TRACE_YELLOW("-------------get gpio pin value -----------------\n");
            TRACE_YELLOW("please input gpio name like: GPIO2:23\n");
            std::cin >> inputStr;
            std::cout << inputStr << std::endl;
			GpioImp* gpio = GpioFactory::getInstance()->getGpioImp(inputStr);
		    if (gpio!=NULL)
		    {
				if(STATUS_ERROR==gpio->config(GPIO::IN))
                {
                    TRACE_RED("config %s IN error.\n",inputStr.c_str());
                    continue;
                }
                int value = gpio->getPinValue();
                if (value!=STATUS_ERROR)
                {
                    TRACE_CYAN("read gpio pin value:%d\n",value );
                }
                else
                {
                    TRACE_RED("read %s value error.\n",inputStr.c_str());
                    continue;
                }
			}
            else
            {
                TRACE_RED("get gpio: %s error.\n",inputStr.c_str());
                continue;
            }
        }
        else if ("set" == inputStr)
        {
            TRACE_YELLOW("-------------set gpio pin value -----------------\n");
            TRACE_YELLOW("please input gpio name like: GPIO2:23\n");
            std::cin >> inputStr;
            std::cout << inputStr << std::endl;
			GpioImp* gpio = GpioFactory::getInstance()->getGpioImp(inputStr);
		    if (gpio!=NULL)
		    {
				if(STATUS_ERROR==gpio->config(GPIO::OUT))
                {
                    TRACE_RED("config %s OUT error.\n",inputStr.c_str());
                    continue;
                }
                TRACE_YELLOW("input value:  0--low, 1--high.\n");

                std::cin >> inputStr;
	            std::cout << inputStr << std::endl;

				/* 设置指定GPIO的pin值 */ 
				if (atoi(inputStr.c_str()) == 0)
				{
				    ret=gpio->setPinValue(GPIO::LOW);
				}
				else
				{
				    ret=gpio->setPinValue(GPIO::HIGH);
				}
                if (ret==STATUS_OK)
                {
                    TRACE_CYAN("write gpio pin value ok.\n");
                }
                else
                {
                    TRACE_RED("write gpio pin value error.\n");
                    continue;
                }
			}
            else
            {
                TRACE_RED("get gpio: %s error.\n",inputStr.c_str());
                continue;
            }
        }
        else
        {
            TRACE_RED("Unknown command: %s !!!\n",inputStr.c_str());
        }
        inputStr.clear();
    }
}
